{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2e1371f8",
   "metadata": {},
   "source": [
    "# Python 运算符\n",
    "\n",
    "1. 算数运算符\n",
    "2. 比较（关系）运算符\n",
    "3. 赋值运算符\n",
    "4. 逻辑运算符\n",
    "5. 位运算符\n",
    "6. 成员运算符\n",
    "7. 身份运算符\n",
    "8. 运算符优先级"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "735c3c7a",
   "metadata": {},
   "source": [
    "## 算数运算符\n",
    "\n",
    "- 加、减、乘、除、取模（`%` 返回除法的余数）、幂、取整除（`//`返回商的整数部分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "b68628a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(30.0, -10.0)"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 加、减\n",
    "a = 10\n",
    "b = 20.0 \n",
    "a+b,a-b\n",
    "# b = '20'\n",
    "# Note: 类型不相符不可相加\n",
    "# a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e25554e1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:15:41.663693Z",
     "start_time": "2022-03-10T03:15:41.641753Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1020'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# str(num) + str;   int + int(str)\n",
    "a = 10\n",
    "b = '20'\n",
    "c = str(a) + b\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "2030182d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(200, 0.5)"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# *, / \n",
    "a*int(b), a/int(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d35ddf0e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:17:24.934834Z",
     "start_time": "2022-03-10T03:17:24.924859Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(3.3333333333333335, 3, 1)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取模, %（返回除法的余数）\n",
    "print(10%3)\n",
    "\n",
    "# 幂, **\n",
    "print(a**3)\n",
    "\n",
    "#取整除, //\n",
    "10/3, 10//3, 10%3   "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af0fef79",
   "metadata": {},
   "source": [
    "## 比较运算符\n",
    "\n",
    "- `==`,  `!=` , `<`,  `>` , `<=`,  `>=`\n",
    "\n",
    "- 结果是一个 布尔 值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "c86650d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# == ; != ; < ; > ; <=, >=\n",
    "a=10\n",
    "b='10'\n",
    "a == b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d4b8c9d1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:32:48.225163Z",
     "start_time": "2022-03-10T03:32:48.204218Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "['A',1] <= ['a',1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4eeaa8f8",
   "metadata": {},
   "source": [
    "## 赋值运算符\n",
    "\n",
    "= 赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a13e23c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3986eece",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:22:20.241436Z",
     "start_time": "2022-03-10T03:22:20.231459Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "# +=, -=, *=, /=\n",
    "a = 10\n",
    "a += 5 # equal  a = a + 5\n",
    "print(a)\n",
    "# -=1\n",
    "a -= 1  # equal a =a-1\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "b20da289",
   "metadata": {},
   "outputs": [],
   "source": [
    "# a++   NO!!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb72481b",
   "metadata": {},
   "source": [
    "## 逻辑运算符\n",
    "\n",
    "and, or, not (取反)\n",
    "\n",
    "逻辑表达式：\n",
    "\n",
    "x and y, x or y, not x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c2a479f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6405754a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b97e95bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True or False\n",
    "# or, 如果前面的为真，后面的就不执行了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ea26a0a3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:26:19.971372Z",
     "start_time": "2022-03-10T03:26:19.949430Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(bool([]))\n",
    "# 整数只有0是False, 其它都为True, 两边都是非0数值，返回后面的数值\n",
    "0 and 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ec1a0eb1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:26:29.744272Z",
     "start_time": "2022-03-10T03:26:29.734270Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool({})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f25aea8f",
   "metadata": {},
   "source": [
    "## 位运算符\n",
    "\n",
    "- 将数值转换成二进制，计算，再返回数值\n",
    "\n",
    "    - &：按位与运算符，参与运算的两个值，如果两个相位都为1，则结果为1，否则为0\n",
    "\n",
    "    - |：按位或运算符，参与运算的两个值，如果两个相位中有一个为1，则结果为1\n",
    "\n",
    "    - ^: 按位异或运算符，当两对应的二进制相异时，结果为1\n",
    "\n",
    "    - \\~: 按位取反运算符，对数据的每个二进制位取反，~1 为0\n",
    "\n",
    "    - <<: 左移运算符，运算数的各二进制位全部左移，移动的位数由 << 右边的数字决定。\n",
    "\n",
    "    - '>>': 右移运算符\n",
    "\n",
    "- 用位运算符计算速度快，效率高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e1c27b94",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:27:36.005813Z",
     "start_time": "2022-03-10T03:27:35.997820Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "print(2**3)\n",
    "1<<3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6d820f96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0b1'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bin(1)  # 转换为2进制，0b 表示二进制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c5304b25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('0b110', '0b101')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bin(6), bin(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3db048eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6 & 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6ae380e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0b100'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bin(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a4bbeb61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7, '0b111')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6 | 5, bin(6|5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5e660869",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('0b1010', '0b11', '0b1001')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bin(10),bin(3),bin(10^3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3a7479f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 1024)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 位运算符计算速度快\n",
    "1<<1, 1<<10\n",
    "#1 移动几位就是2的几次方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "id": "c5eead26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1125899906842624"
      ]
     },
     "execution_count": 198,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1<<50"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b00f82d7",
   "metadata": {},
   "source": [
    "## 成员运算符\n",
    "\n",
    "- in  如果在指定的序列(str, list, set, tuple) 中找到值返回 True, 否则为 False\n",
    "\n",
    "- not in "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "19f51623",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a='jijijijiji'\n",
    "'jiji' in a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "56ab47b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=[1,2,3,4]\n",
    "1 not in a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdd6d656",
   "metadata": {},
   "source": [
    "## 身份运算符 (`is`)\n",
    "\n",
    "- 用于比较两个对象的存储单元\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "id": "7343539f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  var1 is var2\n",
    "a=10\n",
    "b=a\n",
    "a is b  # equl  id(a) == id(b) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "id": "dc315527",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(140727949520928, 140727949520928)"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(b),id(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "id": "8aa68a88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(140727949521248, 140727949521248, 140727949521248)"
      ]
     },
     "execution_count": 201,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=y=z=20  # 在python里是存储在一个地方\n",
    "id(x),id(y),id(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f5b82930",
   "metadata": {},
   "outputs": [],
   "source": [
    "m=[10,20,30]\n",
    "n=m\n",
    "n2=[10,20,30]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "id": "2528ba54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 207,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2 ==m "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "id": "f32ef758",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, False)"
      ]
     },
     "execution_count": 206,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# compare n==m and n is m\n",
    "n is m, n2 is m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cc48be0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([20, 20, 30], [20, 20, 30])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n[0]=20\n",
    "n,m  # m 也会相应进行修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2ab51bb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, False)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m=n.copy()\n",
    "m == n, m is n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
